
package com.shop.cloud.weixin.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.weixin.admin.config.open.WxComponentConfigProperties;
import com.shop.cloud.weixin.admin.config.open.WxOpenConfiguration;
import com.shop.cloud.weixin.common.constant.WxReturnCode;
import com.shop.cloud.weixin.common.entity.WxFastRegist;
import com.shop.cloud.weixin.admin.mapper.WxFastRegistMapper;
import com.shop.cloud.weixin.admin.service.WxFastRegistService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.bean.result.WxOpenResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 快速注册小程序
 *
 * @author
 * @date 2022-02-24 11:55:42
 */
@Service
@AllArgsConstructor
public class WxFastRegistServiceImpl extends ServiceImpl<WxFastRegistMapper, WxFastRegist> implements WxFastRegistService {

	private final WxComponentConfigProperties wxComponentConfigProperties;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(WxFastRegist entity) {
		WxFastRegist wxFastRegist = baseMapper.selectOne(Wrappers.<WxFastRegist>lambdaQuery()
				.eq(WxFastRegist::getName, entity.getName())
				.eq(WxFastRegist::getCode, entity.getCode())
				.eq(WxFastRegist::getLegalPersonaName, entity.getLegalPersonaName())
				.eq(WxFastRegist::getLegalPersonaWechat, entity.getLegalPersonaWechat())
		);
		//如已有记录，则删除
		if(wxFastRegist != null){
			baseMapper.deleteById(wxFastRegist);
		}
		super.save(entity);
		//调用微信接口
		WxOpenResult wxOpenResult = null;
		try {
			wxOpenResult = WxOpenConfiguration.getOpenService().getWxOpenComponentService().fastRegisterWeapp(
					entity.getName(),
					entity.getCode(),
					entity.getCodeType(),
					entity.getLegalPersonaWechat(),
					entity.getLegalPersonaName(),
					wxComponentConfigProperties.getPhone()
			);
		} catch (WxErrorException e) {
			e.printStackTrace();
			Integer errorCode = e.getError().getErrorCode();
			throw new RuntimeException(WxReturnCode.getMsg("ERR_"+errorCode, e));
		}
		if(!WxReturnCode.SUC_0.getCode().equals(wxOpenResult.getErrcode())){
			throw new RuntimeException(wxOpenResult.getErrmsg());
		}
		//查询状态
		this.getStatusById(entity.getId());
		return Boolean.TRUE;
	}

	@Override
	public void getStatusById(String id) {
		WxFastRegist wxFastRegist = baseMapper.selectById(id);
		if(StrUtil.equalsAny(wxFastRegist.getStatus(),WxReturnCode.ERR_89251.getCode(),WxReturnCode.ERR_89252.getCode())){
			//调用微信接口
			WxOpenResult wxOpenResult = null;
			try {
				wxOpenResult = WxOpenConfiguration.getOpenService().getWxOpenComponentService().fastRegisterWeappSearch(
						wxFastRegist.getName(),
						wxFastRegist.getLegalPersonaWechat(),
						wxFastRegist.getLegalPersonaName()
				);
				wxFastRegist.setStatus(wxOpenResult.getErrcode());
				wxFastRegist.setMsg(wxOpenResult.getErrmsg());
			} catch (WxErrorException e) {
				Integer errorCode = e.getError().getErrorCode();
				wxFastRegist.setStatus(String.valueOf(e.getError().getErrorCode()));
				wxFastRegist.setMsg(WxReturnCode.getMsg("ERR_"+errorCode, e));
			}
			//更新状态
			baseMapper.updateById(wxFastRegist);
		}
	}
}
